﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="客户" prop="MID">
          <a-select
                allowClear
                show-search
                v-model="entity.MID"
                placeholder="请选择客户"
              >
                <a-select-option v-for="(item, index) in MerchantList" :key="index" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
        </a-form-model-item>
        <a-form-model-item label="服务商" prop="SID">
          <a-select
                allowClear
                show-search
                v-model="entity.SID"
                placeholder="请选择服务商"
              >
                <a-select-option v-for="(item, index) in ServiceList" :key="index" :value="item.Id">
                  {{ item.ServiceName }}
                </a-select-option>
              </a-select>
        </a-form-model-item>
        <a-form-model-item label="通道" prop="ChannelId">
          <a-select
                allowClear
                show-search
                v-model="entity.ChannelId"
                @change="ChannelChange"
                placeholder="请选择通道"
              >
                <a-select-option v-for="(item, index) in BankcodeList" :key="index" :value="item.Id">
                  {{ item.bankname }}
                </a-select-option>
              </a-select>
        </a-form-model-item>
        <a-form-model-item label="通道名称" prop="ChannelName">
          <a-input v-model="entity.ChannelName" :disabled="true" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="最大支付金额" prop="MaxPayAmount">
          <a-input v-model="entity.MaxPayAmount" :disabled="true" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="日最大支付金额" prop="DayMaxPayAmount">
          <a-input v-model="entity.DayMaxPayAmount" :disabled="true" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="启用状态" prop="OpenStatus">
          <a-radio-group default-value="1" button-style="solid" v-model="entity.OpenStatus">
        <a-radio-button :value="1">
          是
        </a-radio-button>
        <a-radio-button :value="0">
          否
        </a-radio-button>
      </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      MerchantList:[],
      ServiceList:[],
      BankcodeList:[],
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        MID:[{ required: true, message: '请选择客户' }],
        SID:[{ required: true, message: '请选择服务商' }],
        ChannelId:[{ required: true, message: '请选择通道' }],
        ChannelName:[{ required: true, message: '请输入通道名称' }],
        MaxPayAmount:[{ required: true, message: '请输入最大支付金额' }],
        DayMaxPayAmount:[{ required: true, message: '请输入日最大支付金额' }],
      },
      title: ''
    }
  },
  methods: {
    init() {
      this.title = "新增"
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.GetMerchant()
      this.GetService()
      this.GetBankcode()
    },
    openForm(id, title) {
      this.init()

      if (id) {
        this.title = "修改"
        this.loading = true
        this.$http.post('/Base_leasesystem_merchantpaychannel/leasesystem_merchantpaychannel/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    ChannelChange(value){
      let obj = {};
         obj = this.BankcodeList.find((item)=>{ //这里的Data就是上面遍历的数据源
           return item.Id === value; //筛选出匹配数据
         });
      this.entity.ChannelName = obj.bankname
      this.entity.MaxPayAmount = obj.PayMaxAmount
      this.entity.DayMaxPayAmount = obj.DayPayMaxAmount
    },
    handleSubmit() {
      if(this.entity.OpenStatus == null){
        this.entity.OpenStatus = 1;
      }
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_leasesystem_merchantpaychannel/leasesystem_merchantpaychannel/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    async GetMerchant() {
      await this.$http.post('/Base_Manage/Home/GetMerchant_Select').then((res) => {
        this.MerchantList = res.Data
      })
    },
    async GetService() {
      await this.$http.post('/Base_Manage/Home/GetService_Select').then((res) => {
        this.ServiceList = res.Data
      })
    },
    async GetBankcode() {
      await this.$http.post('/Base_Manage/Home/GetBank_Code').then((res) => {
        this.BankcodeList = res.Data
      })
    },
  }
}
</script>
